﻿namespace HiMap.MapControls.Tools
{
    using HiMap.Carto;
    using HiMap.Driver;
    using HiMap.Driver.Ogr;
    using HiMap.Driver.Shape;
    using System;
    using System.Windows.Forms;

    public class CommandAddLayer : CommandClass
    {
        private bool _isEdit;
        private bool _isSpatialIndex;

        public CommandAddLayer()
        {
            this._isSpatialIndex = false;
        }

        public CommandAddLayer(bool isSpatialIndex, bool isEdit)
        {
            this._isSpatialIndex = isSpatialIndex;
            this._isEdit = isEdit;
        }

        protected internal override void OnClick()
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "shp files (*." + ShapeDataFactory.GetExt() + ")|*." + ShapeDataFactory.GetExt() + "|dxf files (*." + OgrDataFactory.GetExt() + ")|*." + OgrDataFactory.GetExt() + "|kvr files (*.kvr)|*.kvr";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                int length = dialog.FileName.LastIndexOf(@"\") + 1;
                switch (dialog.FilterIndex)
                {
                    case 1:
                    {
                        IDataFactory factory = new ShapeDataFactory(dialog.FileName.Substring(0, length));
                        IFeatureClass class2 = (IFeatureClass) factory.Open(dialog.FileName.Substring(length, dialog.FileName.Length - length), this._isEdit);
                        IFeatureLayer layer = new FeatureLayerClass(class2);
                        base.Map.Map.AddLayer(layer);
                        base.Map.MapRefresh();
                        return;
                    }
                    case 2:
                    {
                        IDataFactory factory2 = new OgrDataFactory(dialog.FileName.Substring(0, length));
                        IFeatureClass class3 = (IFeatureClass) factory2.Open(dialog.FileName.Substring(length, dialog.FileName.Length - length), this._isEdit);
                        IFeatureLayer layer2 = new FeatureLayerClass(class3);
                        base.Map.Map.AddLayer(layer2);
                        base.Map.MapRefresh();
                        return;
                    }
                    case 3:
                    {
                        IKRasterLayer layer3 = KRasterLayerClass.Open(dialog.FileName);
                        base.Map.Map.AddLayer(layer3);
                        base.Map.MapRefresh();
                        return;
                    }
                    default:
                        return;
                }
            }
        }
    }
}

